package com.taotao.service.impl;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;
import com.taotao.service.ContentCategoryService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
	@Autowired
	private TbContentCategoryMapper tbContentCategoryMapper;

	public List<EasyUITreeNode> getContentCategoryList(long parentId) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = tbContentCategoryMapper
				.selectByExample(example);

		List<EasyUITreeNode> nodes = new ArrayList<>();
		for (TbContentCategory tbContentCategory : list) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(tbContentCategory.getId());
			node.setText(tbContentCategory.getName());
			node.setState(tbContentCategory.getIsParent() ? "closed"
					: "open");
			nodes.add(node);
		}
		return nodes;
	}

	public TaotaoResult insertCategory(long parentId, String name) {
		TbContentCategory tbContentCategory = new TbContentCategory();
		tbContentCategory.setParentId(parentId);
		tbContentCategory.setName(name);
		tbContentCategory.setStatus(1);
		tbContentCategory.setIsParent(false);
		tbContentCategory.setSortOrder(1);
		tbContentCategory.setCreated(new Date());
		tbContentCategory.setUpdated(new Date());

		tbContentCategoryMapper.insert(tbContentCategory);

		long id = tbContentCategory.getId();

		TbContentCategory parentCategory = tbContentCategoryMapper
				.selectByPrimaryKey(parentId);
		if (!parentCategory.getIsParent()) {
			parentCategory.setIsParent(true);
			parentCategory.setUpdated(new Date());
			tbContentCategoryMapper.updateByPrimaryKey(parentCategory);
		}
		return TaotaoResult.ok(id);
	}

	public TaotaoResult updateCategory(long id, String name) {
		TbContentCategory record = new TbContentCategory();
		record.setName(name);
		record.setId(id);
		record.setUpdated(new Date());
		tbContentCategoryMapper.updateByPrimaryKeySelective(record);
		return TaotaoResult.ok();
	}

	public TaotaoResult deleteCategory(long id) {
		TbContentCategory contentcategory = tbContentCategoryMapper
				.selectByPrimaryKey(id);
		if (contentcategory.getParentId()==0) {
			deleteCategoryListById(id);
			return TaotaoResult.ok();
		}
		long parentId = contentcategory.getParentId();
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = tbContentCategoryMapper
				.selectByExample(example);
		if (list.size() == 1) {
			deleteCategoryListById(id);
			TbContentCategory record = tbContentCategoryMapper
					.selectByPrimaryKey(parentId);

			record.setIsParent(false);
			record.setUpdated(new Date());
			tbContentCategoryMapper.updateByPrimaryKeySelective(record);
			return TaotaoResult.ok();
		}
		deleteCategoryListById(id);
		return TaotaoResult.ok();
	}

	private void deleteCategoryListById(long id) {
		TbContentCategory category = tbContentCategoryMapper
				.selectByPrimaryKey(id);
		if (!category.getIsParent()) {
			tbContentCategoryMapper.deleteByPrimaryKey(id);
		} else {
			TbContentCategoryExample example1 = new TbContentCategoryExample();
			Criteria criteria1 = example1
					.createCriteria();
			criteria1.andParentIdEqualTo(id);
			List<TbContentCategory> list =tbContentCategoryMapper
					.selectByExample(example1);
			for (TbContentCategory tbContentCategory : list) {
				deleteCategoryListById(tbContentCategory.getId());
			}
			tbContentCategoryMapper.deleteByPrimaryKey(id);
		}
	}
}
